/**
 * 
 */
package com.lmm.test;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

/**
 * @author leiming
 *
 */
public class ClientPreemptiveBasicAuthentication {
	   public static void main(String[] args) throws Exception {

	        HttpHost targetHost = new HttpHost("127.0.0.1", 8080, "http");
	        DefaultHttpClient httpclient = new DefaultHttpClient();
	        /*httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
				
				@Override
				public void process(HttpRequest request, HttpContext context)
						throws HttpException, IOException {
					   AuthState authState = (AuthState) context
			                    .getAttribute(ClientContext.TARGET_AUTH_STATE);
			            CredentialsProvider credsProvider = (CredentialsProvider) context
			                    .getAttribute(ClientContext.CREDS_PROVIDER);
			            HttpHost targetHost = (HttpHost) context
			                    .getAttribute(ExecutionContext.HTTP_TARGET_HOST);
			            BasicScheme basicAuth = new BasicScheme();
			            authState.update(basicAuth, new UsernamePasswordCredentials("username", "password"));
			            if(authState!=null){
			            	System.out.println("ok");
			            	//credsProvider.getCredentials(targetHost);
			            }
					
				}
			});*/
	        try {
	        	//AuthScope authScope =  new AuthScope(targetHost.getHostName(), targetHost.getPort());
	            httpclient.getCredentialsProvider().setCredentials(
	            		 new AuthScope(targetHost.getHostName(), targetHost.getPort()),
	                    new UsernamePasswordCredentials("leiming", "240973"));

	            HttpGet httpget = new HttpGet("http://127.0.0.1:8080/test3/test.jsp");
	            System.out.println("executing request: " + httpget.getRequestLine());
	            System.out.println("to target: " + targetHost);

	            for (int i = 0; i < 3; i++) {
	            	  HttpResponse response = httpclient.execute(httpget);
	                //httpclient.setCredentialsProvider(null);
	                HttpEntity entity = response.getEntity();

	                System.out.println("----------------------------------------");
	                System.out.println(response.getStatusLine());
	                if (entity != null) {
	                    System.out.println("Response content length: " + entity.getContentLength());
	                }
	                EntityUtils.consume(entity);
	            }

	        } finally {
	            // When HttpClient instance is no longer needed,
	            // shut down the connection manager to ensure
	            // immediate deallocation of all system resources
	            httpclient.getConnectionManager().shutdown();
	        }
	    }
}
